#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusConnectionInterface>

#include <iostream>

#include "dummyserver.h"

using namespace std;


int main(int argc, char *argv[])
{
	cout << "DummyPowerboxServer" << endl;

	if(argc < 3)
	{
		cerr << "Usage:\ndummypowerboxserver file1 file2" << endl;
		return 1;
	}

	QStringList filenames;
	for(int i = 1; i < argc; i++) filenames << argv[i];

	QApplication app(argc, argv);

	QString serviceName = "org.freebsd.capsicum.dummy";

	QDBusConnection dbus = QDBusConnection::sessionBus();

	if(!dbus.isConnected())
	{
		cerr << "Not connected the D-Bus session bus: "
		     << dbus.lastError().message().toStdString()
		     << endl;
		return 1;
	}

	if(dbus.registerService(serviceName))
		cout << "Registered " << serviceName.toStdString();

	else
	{
		cerr << "Error registering " << serviceName.toStdString() << ": "
		     << dbus.lastError().message().toStdString() << endl;
		return 1;
	}

	DummyPowerboxServer server(filenames);
	QString path = "/PowerboxServer";
	if(dbus.registerObject(path, &server, QDBusConnection::ExportAllSlots))
		cout << path.toStdString() << endl;

	else
	{
		cerr << "Failed to register " << path.toStdString() << ":"
		     << dbus.lastError().message().toStdString() << endl;

		return 1;
	}

	int retval = 0;
	while(!retval)
	{
		retval = app.exec();
		if(retval)
		{
			cerr << "Abnormal app.exec() result: " << retval << endl;
			return retval;
		}
	}

	return 0;
}

